package hibari.server.operations;

import hibari.client.model.Music;
import hibari.client.model.Week;
import hibari.server.lib.AdminLog;
import hibari.server.lib.FileData;
import hibari.server.lib.MultiPartRequest;
import hibari.server.model.ImageData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Blob;

public class EditWeek extends PMOperation {

	public EditWeek(MultiPartRequest request, HttpServletResponse response) {
		super(request, response);
	}

	@Override
	public void doOperation() {
		Week week = getTargetWeek();

		updateWeek(week);

		Music music1 = getPM().getObjectById(Music.class, week.getMusic1());
		Music music2 = getPM().getObjectById(Music.class, week.getMusic2());

		updateMusic(music1, 1);
		updateMusic(music2, 2);

		getPM().makePersistent(week);
		getPM().makePersistent(music1);
		getPM().makePersistent(music2);

		getPM().close();

		AdminLog.get(getRequest()).addInfo("An Week has been updated!");
	}

	protected void updateWeek(Week week) {
		String dateExp = getRequest().getParameter("date");
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = f.parse(dateExp);
			week.setDate(date);
		} catch (ParseException e) {
			AdminLog.get(getRequest()).addError("date must be yyyy-MM-dd");
			e.printStackTrace();
		}

		try {
			week.setWeekNumber(Integer.parseInt(getRequest().getParameter("week_no")));
		} catch (Exception e) {
			AdminLog.get(getRequest()).addError("Week No must be integer.");
			e.printStackTrace();
		}

		if ("remove".equals(getRequest().getParameter("remove_week_image"))) {
			week.setImageId(null);
		} else {
			FileData weekImageData = getRequest().getData("week_image");
			if (weekImageData != null && weekImageData.isSet()) {
				ImageData imageData = new ImageData();
				imageData.setFilename(weekImageData.getFilename());
				imageData.setData(new Blob(weekImageData.getData()));
				getPM().makePersistent(imageData);
				week.setImageId(imageData.getId());
			}
		}
//
//		FileData weekIconData = getRequest().getData("week_icon");
//		if (weekIconData != null && weekIconData.isSet()) {
//			ImageData imageData = new ImageData();
//			imageData.setFilename(weekIconData.getFilename());
//			imageData.setData(new Blob(weekIconData.getData()));
//			getPM().makePersistent(imageData);
//			week.setIconId(imageData.getId());
//		}

		String weekColor = getRequest().getParameter("week_color");
		if (weekColor != null && !weekColor.isEmpty()) {
			week.setBackgroundColor(weekColor);
		}
	}

	protected void updateMusic(Music music, int i) {
		music.setTitleInEnglish(getMusicParameter(i, "title_en"));
		music.setTitleInJapanese(getMusicParameter(i, "title_jp"));

		FileData musicFileData = getMusicFileData(i, "image");
		if (musicFileData.isSet()) {
			ImageData imageData = new ImageData();
			imageData.setFilename(musicFileData.getFilename());
			imageData.setData(new Blob(musicFileData.getData()));
			getPM().makePersistent(imageData);
			music.setImageId(imageData.getId());
		}

		music.setComposerInEnglish(getMusicParameter(i, "composer_en"));
		music.setComposerInJapanese(getMusicParameter(i, "composer_jp"));

		music.setComposerProfileInEnglish(getMusicParameter(i, "composer_profile_en"));
		music.setComposerProfileInJapanese(getMusicParameter(i, "composer_profile_jp"));

		music.setCompositionInEnglish(getMusicParameter(i, "composition_en"));
		music.setCompositionInJapanese(getMusicParameter(i, "composition_jp"));

		music.setDescriptionInEnglish(getMusicParameter(i, "description_en"));
		music.setDescriptionInJapanese(getMusicParameter(i, "description_jp"));

		music.setDemoUrl(getMusicParameter(i, "demo_url"));
	}

	protected String getMusicParameter(int i, String name) {
		return getRequest().getParameter("music" + i + "_" + name);
	}

	protected FileData getMusicFileData(int i, String name) {
		return getRequest().getData("music" + i + "_" + name);
	}

	protected Week getTargetWeek() {
		String id = getRequest().getParameter("id");
		Week week = null;
		try {
			week = getPM().getObjectById(Week.class, Long.parseLong(id));
		} catch (Exception e) {
			week = null;
		}
		return week;
	}

}
